package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.CommunityDao;
import com.xueyu.dao.HouseDao;
import com.xueyu.dao.HousepicDao;
import com.xueyu.dao.TermDao;
import com.xueyu.domain.HouseBean;
import com.xueyu.domain.HousepicBean;
import com.xueyu.domain.SupplierBean;
import com.xueyu.domain.TermBean;
import com.xueyu.domain.bed.Community;
import com.xueyu.domain.drug.drug;
import com.xueyu.domain.drugs.drugsBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Housepic")
public class HousepicController {
    @Autowired
    HousepicDao housepicDao;

    @Autowired
    TermDao termDao;

    @Autowired
    CommunityDao communityDao;

    @Autowired
    HouseDao houseDao;

    @RequestMapping(value = "/add")
    public ResultBean add(@ModelAttribute HousepicBean housepicBean,HttpServletRequest request, @RequestPart(value = "pic", required = false) MultipartFile[] pic) throws Exception{
        ResultBean resultBean = new ResultBean();
        if(pic!=null) {
            for (MultipartFile file : pic) {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)-
                String oriName = file.getOriginalFilename();
                String extName = oriName.substring(oriName.lastIndexOf("."));
                String path = request.getSession().getServletContext().getRealPath("\\");
                //日期处理
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String sdate = sdf.format(date);
                String dirName = "\\upload\\" + sdate + "\\";
                File dirFile = new File(path + dirName);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                String newFileName = picName + extName;
                File targetFile = new File(path + dirName, newFileName);
                file.transferTo(targetFile);
                String path1 = dirName + newFileName;
                System.out.println("path=" + path1);

                housepicBean.setUrl(path1);
            }
        }

        if(housepicBean.getPicId()==0) {
            housepicDao.insert(housepicBean);
        }else {
            housepicDao.updateById(housepicBean);
        }
        return resultBean;
    }

    //分页.条件查询.
    @RequestMapping("/list")
    public ResultBean list(@RequestBody Map map) {
        int curPage = (Integer)map.get("curPage");
        int size = (Integer) map.get("size");
        Page<HousepicBean> pager = new Page<HousepicBean>(curPage, size);
        List<HousepicBean> housepiclist = housepicDao.listPage(pager, map);
        List<Community> communityList = communityDao.selectList(null);
        List<TermBean> termList = termDao.selectList(null);
        List<HouseBean> houseList = houseDao.selectList(null);
        pager.setRecords(housepiclist);
        ResultBean resultBean = new ResultBean();
        resultBean.put("pager",pager);
        resultBean.put("housepiclist",housepiclist);
        resultBean.put("communityList",communityList);
        resultBean.put("termList",termList);
        resultBean.put("houseList",houseList);
        return resultBean;
    }

    //删除
    @RequestMapping("/del/{picId}")
    public ResultBean del(@PathVariable("picId") int picId){
        housepicDao.deleteById(picId);
        return new ResultBean();
    }

    @RequestMapping("/getId/{picId}")
    public ResultBean getId(@PathVariable("picId") int picId){
        Map map = new HashMap();
        ResultBean resultBean = new ResultBean();
        if (picId>0) {
            HousepicBean housepicList = housepicDao.selectById(picId);
            map.put("housepicList", housepicList);
        }
        List<Community> communityList = communityDao.selectList(null);
        List<TermBean> termList = termDao.selectList(null);
        List<HouseBean> houseList = houseDao.selectList(null);
        map.put("communityList",communityList);
        map.put("termList",termList);
        map.put("houseList",houseList);
        resultBean.put("data",map);
        return resultBean;
    }
}
